# @ModuleName: browseoperation
# @Author: zxl
# @Time: 2024/6/15 9:38'
from selenium.webdriver.common.by import By

from quote.base.usebrowserteacher import UseBrowser


class BrowserOperation:



    def __init__(self,driver):
        self.driver = driver

    def search_element(self,type,locator):
        # if type.lower()
        by_type = {'id': By.ID,
                   'name': By.NAME,
                   'xpath': By.XPATH,
                   'link_text': By.LINK_TEXT,
                   'partial_link_text':By.PARTIAL_LINK_TEXT,
                   'tag_name': By.TAG_NAME,
                   'class_name': By.CLASS_NAME,
                   'css_selector':By.CSS_SELECTOR}.get(type.lower())
        # 检查是否找到了对应的 By 类型
        if by_type is None:
            raise ValueError(
                f"Invalid type: {type}. Expected one of ['id', 'name', 'xpath', 'link_text', 'partial_link_text', 'tag_name', 'class_name', 'css_selector']")

        # 使用找到的 By 类型和 locator 来定位元素
        element = self.driver.find_element(by_type, locator)
        return element
    # 输入操作
    def input_text(self,type,locator,text):
        self.search_element(type,locator).send_keys(text)

    # 点击操作
    def click_element(self,type,locator):
        self.search_element(type,locator).click()

    # 获取操作
    def get_text(self,type,locator):
        return self.search_element(type,locator).text

    # 切换frame
    def change_frame(self,name):
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(name)

    def open_url(self,url):
        self.driver.get(url)

    def change_window(self, title):
        for i in self.driver.window_handles:
            self.driver.switch_to.window(i)
            if self.driver.title == title:
                break





